package com.zhongxi.guoyuan.view;

import android.view.View;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.ad.JDMoreAdapter;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsData;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsResult;
import com.zhongxi.guoyuan.url.Url;

import java.util.ArrayList;
import java.util.List;

public class MoreAdActivity extends BaseActivity implements View.OnClickListener {

    private PullToRefreshListView lvList;
    private List<HotGoodsData> datas = new ArrayList<>();
    private JDMoreAdapter adapter;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_more_ad);
        initTitle();
        tvTitle.setText("特别推荐");
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new JDMoreAdapter(context, datas);
        lvList.setAdapter(adapter);

        getServer(Url.HOT_GOODS, null, "", false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            HotGoodsResult hotResult = gson.fromJson(response, HotGoodsResult.class);
            MyApplication.getInstance().setSpValue(hotResult.getSafeobj());
            if(hotResult.getData().size()>0){
                datas.addAll(hotResult.getData());
            }
            lvList.onRefreshComplete();
            adapter.notifyDataSetChanged();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj()))
                MyApplication.getInstance().setSpValue(result.getSafeobj());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }
}
